package com.security.demo.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import com.fasterxml.jackson.annotation.JsonView;
import com.security.demo.entity.UserEntity;
import com.security.demo.exception.UserNotExistException;
import com.security.demo.vo.UserQueryCondition;

/**
 * <p>Title: UserController</p>
 * <p>Description: </p> 
 * @author liukang
 * @date 2019年4月18日
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private ProviderSignInUtils providerSignInUtils;
	
	@PostMapping("/regist")
	public void regist(UserEntity user,HttpServletRequest request) {
		//注册用户
		String userId = user.getUsername();
		providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
		
	}
	
	@GetMapping("/me")
	public Object getCurrentUser() {
		return SecurityContextHolder.getContext().getAuthentication();
	}
	
	@GetMapping("/me1")
	public Object getCurrentUser1(Authentication authentication) {
		return authentication;
	}
	
	@GetMapping("/me2")
	public Object getCurrentUser2(@AuthenticationPrincipal UserDetails user) {
		return user;
	}
	
	@GetMapping
	@JsonView(UserEntity.UserSimpleView.class)
	public List<UserEntity> query(UserQueryCondition condition){
		System.out.println(condition.getUsername() + ":" + condition.getAge());
		System.out.println(ReflectionToStringBuilder.toString(condition,ToStringStyle.MULTI_LINE_STYLE));
		List<UserEntity> list = new ArrayList<>();
		list.add(new UserEntity());
		list.add(new UserEntity());
		list.add(new UserEntity());
		return list;
	}
	
	@GetMapping("/{id:\\d+}")
	@JsonView(UserEntity.UserDetailView.class)
	public UserEntity getUserDetailInfo(@PathVariable String id) {
//		throw new UserNotExistException(id);
		System.out.println("进入getInfo");
		UserEntity entity = new UserEntity();
		entity.setUsername("tom");
		return entity;
	}
	
	@DeleteMapping("/{id:\\d+}")
	public void delete(@PathVariable String id) {
		System.out.println(id);
	}
	
	@PostMapping
	public UserEntity addUserEntity(@Valid @RequestBody UserEntity entity ,BindingResult error) {
		if (error.hasErrors()) {
			error.getAllErrors().forEach(i -> System.out.println(i.getDefaultMessage()));
		}
		System.out.println(ReflectionToStringBuilder.toString(entity,ToStringStyle.MULTI_LINE_STYLE));
		entity.setId("1");
		return entity;
	}
	
	@PutMapping("/{id:\\d+}")
	public UserEntity updateUserEntity(@Valid @RequestBody UserEntity entity ,BindingResult error) {
		if (error.hasErrors()) {
			error.getAllErrors().forEach(i -> System.out.println(i.getDefaultMessage()));
		}
		System.out.println(ReflectionToStringBuilder.toString(entity,ToStringStyle.MULTI_LINE_STYLE));
		entity.setId("1");
		return entity;
	}
}
